package com.design.testdemo;

import android.os.Handler;
import android.os.Message;

/**
 * 时间监听
 */

public class TimerUtils {

    // 总的开始时间
    private long totalTime = 0;
    private long totalCurrTime = 0;

    // 副的开始时间
    private long deputyTime = 0;
    private long deputyCurrTime = 0;

    // 控制暂停
    private boolean pause_totalTime = false;
    private boolean pause_deputyTime = false;

    private Thread thread; // 控制时间的子线程
    private static TimerUtils instance;// 创建单例
    private TimeListener mTimeListener;// 设置时间的监听器

    private TimerUtils() {
    }

    public static TimerUtils getInstance() {
        if (instance == null) {
            instance = new TimerUtils();
        }
        return instance;
    }

    private void init() {
        totalTime = 0;
        totalCurrTime = 0;
        deputyTime = 0;
        deputyCurrTime = 0;

        pause_totalTime = false;
        pause_deputyTime = false;

        deputyPauseTime = 0;
        totalPauseTime = 0;
    }

    public Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // 在UI中设置时间
            if (mTimeListener != null) {
                mTimeListener.onTimeChange(totalCurrTime, deputyCurrTime);
            }
        }
    };

    public void initTime() {
        if (thread == null) {
            totalTime = System.currentTimeMillis();
            deputyTime = System.currentTimeMillis();

            thread = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (totalTime != System.currentTimeMillis()) {

                            if (!pause_totalTime) {
                                totalCurrTime = System.currentTimeMillis() - totalTime;
                            }

                            if (!pause_deputyTime) {
                                // 当前不暂停才进来
                                deputyCurrTime = System.currentTimeMillis() - deputyTime;
                            }
                        }
                        handler.sendEmptyMessage(1);
                    }
                }
            };
            thread.start();
        }
    }

    public interface TimeListener {
        void onTimeChange(long totalCurrTime, long deputyCurrTime);
    }

    public void setTimeListener(TimeListener mTimeListener) {
        this.mTimeListener = mTimeListener;
    }

    // ======================= 暂停 ======================

    private long deputyPauseTime = 0;

    // 暂停副时间
    public void setPause_deputyTime(boolean pause) {

        if (pause == pause_deputyTime) {
            return;
        }

        if (pause) {
            // 暂停
            // 记录当前时间
            deputyPauseTime = System.currentTimeMillis();
        } else {
            // 取消暂停
            // 记录当前时间 同时startTime 加上
            deputyPauseTime = System.currentTimeMillis() - deputyPauseTime;
            deputyTime += deputyPauseTime;
            deputyPauseTime = 0;
        }

        pause_deputyTime = pause;
    }

    private long totalPauseTime = 0;

    // 暂停总时间
    public void setPause_totalTime(boolean pause) {

        if (pause == pause_totalTime) {
            return;
        }

        if (pause) {
            // 暂停
            // 记录当前时间
            totalPauseTime = System.currentTimeMillis();
        } else {
            // 取消暂停
            // 记录当前时间 同时startTime 加上
            totalPauseTime = System.currentTimeMillis() - totalPauseTime;
            totalTime += totalPauseTime;
            totalPauseTime = 0;
        }

        this.pause_totalTime = pause;
    }

    // 结束
    public void stopTimer() {
        handler.removeCallbacks(thread);
        mTimeListener = null;
        thread = null;
        init();
    }
}
